/**
 * Created by Administrator on 2016/8/6.
 */
"use strict";
swan_pc
    .controller("ActivityDetailController",function($scope,$q,$state,getInvitationById,EventManageById,ContactInfo,candidateUserByEvent,
                                                    CommentManage,smallEventManage,CandidateUsersInSmallEvent,sessionManage,getCandidateUser,GetAccountInfo,
                                                    BigEventById,InvitationConflicts,ClipPost,InvitationConfirm,InvitationCancel){
        //获取当前账号信息
        GetAccountInfo.get(function(result){
            $scope.currentAccountId=result.id;
        });
        $scope.presentActivity={};
        $scope.presentEventDetail={};
        $scope.candidateUsersList=[];
        //enroll状态展示
        $scope.userEnrollStatus_show=function(option){
            var enrollStatusName="";
            if(option=="PENDING"){
                enrollStatusName="待定";
            }else if(option=="CONFIRM"){
                enrollStatusName="确认参加";
            }else if(option=="CANCEL"){
                enrollStatusName="拒绝参加";
            }
            return enrollStatusName;
        };
        //cantact姓名展示
        ContactInfo.query(function(result){
            $scope.contactCurrentInfo=result;
        })
        $scope.contactNameShow=function(option){
            var contactName="";
            angular.forEach($scope.contactCurrentInfo,function(data,index,array){
                if(data.id==option){
                    contactName=data.baseInfo.name;
                }
            })
            return contactName;
        }
        //activity 展示内容变换
        $scope.activeNumber=1;
        $scope.changeActivityShow=function(option){
            $scope.activeNumber=option;
        }
        $scope.BackToActivityList=function(){
            $state.go("activitylist");
        }
        //邀请函展示
        $scope.stationeryUrlList={
            "信纸1":"http://www.diamondtiming.com:8080/uploads/20160809131748_xin0.jpg",
            "信纸2":"http://www.diamondtiming.com:8080/uploads/20160809131803_xin1.jpg",
            "信纸3":"http://www.diamondtiming.com:8080/uploads/20160809131809_xin2.jpg",
            "信纸4":"http://www.diamondtiming.com:8080/uploads/20160809131815_xin3.jpg",
            "信纸5":"http://www.diamondtiming.com:8080/uploads/20160809131823_xin4.jpg"
        };
        var def=$q.defer();
        var pro=def.promise;
        getInvitationById.get({id:$scope.$parent.activityDetailId},function(result){
            $scope.presentActivity=result;
            //console.log($scope.presentActivity.activityType);
            if($scope.presentActivity.activityType=="INDIVIDUALEVENT") {
                EventManageById.get({id: $scope.presentActivity.activityId}, function (result) {
                    $scope.presentEventDetail = result;
                    //console.log(result);
                    def.resolve();
                    //console.log($scope.presentEventDetail);
                }, function (result) {
                    //console.log(result);
                });
                candidateUserByEvent.query({id:$scope.presentActivity.activityId},function(result){
                    $scope.candidateUsersList=result;
                })
            }else if($scope.presentActivity.activityType=="SMALLEVENT"){
                smallEventManage.get({id:$scope.presentActivity.activityId},function(result){
                    $scope.presentEventDetail = result;
                    def.resolve();
                },function(result){

                });
                CandidateUsersInSmallEvent.query({id:$scope.presentActivity.activityId},function(result){
                    $scope.candidateUsersList=result;
                })
            }else if($scope.presentActivity.activityType=="EVENT"){
                BigEventById.get({id:$scope.presentActivity.activityId},function(result){
                    $scope.presentEventDetail = result;
                    def.resolve();
                })
            }

        })
        pro.then(
            function(){
                $scope.initCommentDisplay();
                if($scope.presentEventDetail.stationeryUrl.length>5){
                    $scope.invationStyle = {
                        "position": 'center',
                        "background-image": 'url(http://www.diamondtiming.com:8080'+$scope.presentEventDetail.stationeryUrl+')',
                        "background-size": 'cover',
                        "width": '100%',
                        "padding":"20% 0"
                    };
                    console.log($scope.invationStyle['background-image']);
                }else {
                    for (var key in $scope.stationeryUrlList) {
                        if ($scope.presentEventDetail.stationeryUrl == key) {
                            var imageUrl = $scope.stationeryUrlList[key];
                            var imageUrl = imageUrl.split(".");
                            var imageUrlLength = imageUrl[2].length;
                            var imageFlag = imageUrl[2][imageUrlLength - 1];
                            if (imageFlag == 0) {
                                $scope.invationStyle = {
                                    "position": 'center',
                                    "background-image": 'url(' + $scope.stationeryUrlList[key] + ')',
                                    "background-size": 'cover',
                                    "width": '100%',
                                    "padding": "20% 0"
                                }
                            } else if (imageFlag == 1) {
                                $scope.invationStyle = {
                                    "position": 'center',
                                    "background-image": 'url(' + $scope.stationeryUrlList[key] + ')',
                                    "background-size": 'cover',
                                    "width": '100%',
                                    "padding": "20% 0",
                                    "color": "white",
                                    "text-shadow": "5px 5px 5px black"
                                }
                            } else if (imageFlag == 2) {
                                $scope.invationStyle = {
                                    "position": 'center',
                                    "background-image": 'url(' + $scope.stationeryUrlList[key] + ')',
                                    "background-size": 'cover',
                                    "width": '100%',
                                    "padding": "30% 0",
                                    "color": "white",
                                }
                            } else if (imageFlag == 3) {
                                $scope.invationStyle = {
                                    "position": 'center',
                                    "background-image": 'url(' + $scope.stationeryUrlList[key] + ')',
                                    "background-size": 'cover',
                                    "width": '100%',
                                    "padding": "20% 0",
                                    "color": "white"
                                }
                            } else {
                                $scope.invationStyle = {
                                    "position": 'center',
                                    "background-image": 'url(' + $scope.stationeryUrlList[key] + ')',
                                    "background-size": 'cover',
                                    "width": '100%',
                                    "padding": "20% 0",
                                    "color": "#EEC900",
                                    "text-shadow": "3px 3px 3px blue"
                                    //"filter":"shadow(Color=#0000ff,Direction=10,Strength=10)"
                                }
                            }
                        }
                    }
                }
            }
        )
        $scope.setDivHeight=function(){
            var w=$('#invationContent').width();
            //$('#invationContent').attr('height',w*1.77);
            var height=w*1.3;
            document.getElementById("invationContent").style.height=height+"px";
            var h=$('#invationContent').height();
            //console.log("run: "+w);
            //console.log("height:"+h);
        }
        $scope.setDivHeight();
        //讨论模块实现
        $scope.newComment={
            activityId:0,
            activityType:"INDIVIDUALEVENT",
            content:null
        }
        $scope.allComments=[];
        $scope.initCommentDisplay=function() {
            $scope.commentsForCurrentEvent=[];
            CommentManage.query(function (result) {
                $scope.allComments = result;
                //console.log("allComments");
                //console.log($scope.allComments);
                angular.forEach($scope.allComments, function (data, index, array) {
                    //console.log($scope.presentActivity.activityId);
                    //console.log(data.activityType + "     "+data.activityId );
                    if (data.activityType == $scope.presentActivity.activityType && data.activityId == $scope.presentActivity.activityId) {
                        $scope.commentsForCurrentEvent.push(data);
                    }
                })
            },function(result){
            })
            $scope.col = "time";
            $scope.desc = 0;
        }
        $scope.submitComment=function(){
            $scope.newComment.activityId=$scope.presentActivity.activityId;
            $scope.newComment.activityType=$scope.presentActivity.activityType;
            CommentManage.save($scope.newComment,function(result){
                $scope.initCommentDisplay();
                swal("提交评论成功","","success");
            },function(result){
                //console.log(result);
                swal("提交评论失败","","error");
            })
        }
        //session列表实现
        pro.then(function(){
            $scope.sessionView=[];
            var def1=$q.defer();
            var pro1=def1.promise;
            sessionManage.query({},function(result){
                $scope.sessionList=result;
                {
                    _.forEach($scope.sessionList,function(data,index,array){
                        if(data.eventId==$scope.presentEventDetail.id){
                            $scope.sessionView.push(data);
                        }
                    });
                    def1.resolve();
                }
            });
            pro1.then(function(){
                $scope.getAllCandidateUsers();
            });
        });
        $scope.getAllCandidateUsers=function(){
            $scope.currentSessionInvitation=[];
            getCandidateUser.query(function(result){
                for(var i=0;i<result.length;i++){
                    //console.log(result[i].activityType+"  "+result[i].userId+"   "+$scope.currentAccountId);
                    if(result[i].activityType=="SESSION" && result[i].userId==$scope.currentAccountId){
                        //console.log(result[i]);
                        for(var j=0;j<$scope.sessionView.length;j++){
                            //console.log(result.activityId+"   "+$scope.sessionView[j].id);
                            if(result[i].activityId==$scope.sessionView[j].id){
                                $scope.currentSessionInvitation.push(result[i]);
                            }
                        }
                    }
                }
            })
        };
        //approveStatus展示
        $scope.approveStatus_show=function(option){
            var name="";
            if(option=="PENDING"){
                name="待定";
            }else if(option=="WAITING"){
                name="排队等待";
            }else if(option=="CONFIRM"){
                name="已批准";
            }else if(option=="REJECT"){
                name="未通过";
            }else if(option=="CANCEL"){
                name="取消";
            }
            return name;
        };
        //enroll状态展示
        $scope.userEnrollStatus_show=function(option){
            //console.log(option);
            var enrollStatusName="";
            if(option=="PENDING"){
                enrollStatusName="待定";
            }else if(option=="CONFIRM"){
                enrollStatusName="确认参加";
            }else if(option=="REJECT"){
                enrollStatusName="拒绝参加";
            }else if(option=="CANCEL"){
                enrollStatusName="取消";
            }
            return enrollStatusName;
        }
        //状态跳转至session详情
        $scope.changeStateToSessionDetail=function(id){
            $scope.$parent.sessionId=id;
            $scope.$parent.envolopeUrl=$scope.presentEventDetail.stationeryUrl;
            $state.go("sessionDetail");
        }
        //session邀请处理
        //接受活动邀请
        $scope.conflictClips=[];
        $scope.newClip={
            "actionRemark": null,
            "address": null,
            "advanceInfo": null,
            "category": null,
            "checkList": null,
            "code": null,
            "color": null,
            "deletedAt": null,
            "endTime": null,
            "individualTaskId": null,
            "isDelayed": null,
            "isFixed": null,
            "isImportant": null,
            "isPrincipal": null,
            "isSecret": null,
            "level": null,
            "linkedActivityId": null,
            "linkedActivityType": null,
            "name": null,
            "organizationId": null,
            "ownerId": null,
            "previewTime": null,
            "relatedCompany": null,
            "relatedContact":null,
            "relatedOpp": null,
            "remark": null,
            "remind": null,
            "startTime":null,
            "status":null,
            "type": null,
            "who": null
        }
        $scope.confirmToSession=function(id){
            var def=$q.defer();
            var pro=def.promise;
            InvitationConflicts.query({id:id},function(result){
                $scope.conflictClips=result;
                $scope.presentSessionInvite={};
                $scope.presentEventDetail={};
                getInvitationById.get({id:id},function(result) {
                    $scope.presentSessionInvite = result;
                    sessionManage.get({id: $scope.presentActivity.activityId}, function (result) {
                        $scope.presentEventDetail = result;
                        def.resolve();
                    })
                })
                pro.then(function(){
                    if($scope.conflictClips.length>0){
                        swal("您当前的clip与此项邀请冲突","请处理后再接受此邀请","error");
                    }else{
                        InvitationConfirm.update(
                            {id:id},function(){
                                $scope.getAllCandidateUsers();
                                $scope.newClip.name=$scope.presentEventDetail.name;
                                $scope.newClip.startTime=$scope.presentEventDetail.startTime;
                                $scope.newClip.endTime=$scope.presentEventDetail.endTime;
                                $scope.newClip.address=$scope.presentEventDetail.address;
                                $scope.newClip.status="CONFIRM";
                                $scope.newClip.remark=$scope.presentEventDetail.remark;
                                $scope.newClip.level="主事项";
                                $scope.newClip.type=$scope.presentEventDetail.type;
                                ClipPost.save($scope.newClip,function(result){
                                    console.log(result);
                                    swal("您已接受好友活动邀请","","success");
                                },function(result){
                                    console.log(result);
                                })
                            },
                            function(result){
                                console.log(result);
                                swal("接受好友邀请失败",result.data.description,"error");
                            }
                        )
                    }
                })
            })
        }
        //拒绝活动邀请
        $scope.cancelToSession=function(id){
            InvitationCancel.update(
                {id:id},function(){
                    $scope.init();
                    swal("您已拒绝好友活动邀请","","success");
                },function(result){
                    swal("拒绝好友邀请失败","","error");
                }
            )
        }
    });